function CC = selectCommon(Q, from, V)

%this is a common function to generate any S3QL select queries given:
%   Q.key
%   Q.url
%and where do u want it from, thus
%   from  = rules,collections,items,statements,rulelog,users,groups,
%            keys, permission or any new select feature
%and V is a struct with fieldnames being the respective names that go into
%an where statement. The fieldnames should match with s3ql syntax
%For example for from = rules, V struct can be
%   V.rule_id = ff
%   V.subject = ff
%   V.project_id = ff

%R0mesh Stanslaus May 2009 //summer of chage//

%check to see if V is a struct
if not(isstruct(V))
    error('The third arg should be a struct...see help')
end

%get filednames
fNames = fieldnames(V);

%generate query body
body='';
for i = 1:size(fNames,1)
    try
        body = [body,'<',fNames(i),'>',num2str(getfield(V,fNames{i})),'</',fNames(i),'>'];
    catch
        body = [body,'<',fNames(i),'>',getfield(V,fNames{i}),'</',fNames(i),'>'];
    end
    %body = [body,b1];
end
%combine the cell contents together
body=[body{1,:}];

%construct S3QL query
front = ['<S3QL><select>*</select><from>',lower(from),'</from><where>'];
back = '</where></S3QL>';

S1 = [front,body,back];

Q.X = S1;
try
    [D1,Q]=s3db_read(Q); % -2
catch
    error('Please check your syntax to s3ql and try again...')
end
CC = D1;

